package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WOrder;
import com.ruoyi.system.domain.vo.WGoodExcel;
import com.ruoyi.system.domain.vo.WOrderVO;
import com.ruoyi.system.mapper.WGoodMapper;
import com.ruoyi.system.mapper.WOrderGoodMapper;
import com.ruoyi.system.service.IWOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 物料订单Controller
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Api(value = "工厂物料订单",tags = "物料订单")
@RestController
@RequestMapping("/system/order/factory")
public class WOrderFactoryController extends BaseController
{
    @Autowired
    private IWOrderService wOrderService;
    @Autowired
    private WGoodMapper wGoodMapper;
    @Autowired
    private WOrderGoodMapper wOrderGoodMapper;
    
    
    @ApiOperation("工厂导入订单  返回的number 为库存数量")
    @PostMapping("importDataGood/{cutomerId}")
    public AjaxResult importDataFactory(MultipartFile file, boolean updateSupport,@PathVariable("customerId") Long customerId) throws Exception
    {
    	LoginUser loginUser = getLoginUser();
    	Long deptId = loginUser.getDeptId();
        ExcelUtil<WGoodExcel> util = new ExcelUtil<WGoodExcel>(WGoodExcel.class);
        List<WGoodExcel> wGoodExcelList = util.importExcel(file.getInputStream());
        if (StringUtils.isEmpty(wGoodExcelList)) return AjaxResult.error("没有物料信息");
        
        for (WGoodExcel e : wGoodExcelList) {
        	if (StringUtils.isBlank(e.getComment()) 
        			||StringUtils.isBlank(e.getPn()) 
        			||StringUtils.isBlank(e.getComment()) 
        			||StringUtils.isBlank(e.getDesignator()) 
        			||StringUtils.isBlank(e.getDimension()) 
        			||StringUtils.isBlank(e.getFootprint()) 
        			||StringUtils.isBlank(e.getManufacture()) 
        			) {
        		 return AjaxResult.error("不能传空参数");
			}
		}
        return AjaxResult.success(wOrderService.importDataGood(wGoodExcelList, customerId, deptId));
    }
    
    @ApiOperation("工厂 新增物料订单")
    @PostMapping("addOrder")
    public AjaxResult addFactory(@RequestBody WOrderVO wOrderVO)
    { 
    	LoginUser loginUser = getLoginUser();
    	Long deptId = loginUser.getDeptId();
    	wOrderVO.setDeptId(deptId);
    	wOrderVO.setCreateBy(loginUser.getUsername());
    	if (StringUtils.isEmpty(wOrderVO.getOrderGoodList())) return AjaxResult.error("没有物料信息");
        return toAjax(wOrderService.insertWOrder(wOrderVO));
    }
    
    @ApiOperation("工厂 修改物料订单")
    @PostMapping("updateOrder")
    public R updateOrder(@RequestBody WOrderVO wOrderVO)
    { 
    	LoginUser loginUser = getLoginUser();
    	Long deptId = loginUser.getDeptId();
    	wOrderVO.setDeptId(deptId);
    	wOrderVO.setUpdateBy(loginUser.getUsername());
    	wOrderVO.setUpdateTime(new Date());
    	if (StringUtils.isEmpty(wOrderVO.getOrderGoodList())) return R.error("没有物料信息");
    	return  wOrderService.updateWOrder(wOrderVO);
    }

    @ApiOperation("生产计划接口")
    @PostMapping("addOrderPlan")
    public R<WOrder> addOrderPlan(@RequestBody WOrderVO wOrderVO)
    { 
    	return wOrderService.addOrderPlan(wOrderVO);
    }


    /**
     * 查询物料订单列表
     */
    @ApiOperation("工厂查询物料订单")
    @GetMapping("/list")
    public TableDataInfo list(WOrderVO wOrder)
    {
    	LoginUser loginUser = getLoginUser();
    	wOrder.setDeptId(loginUser.getDeptId());
        startPage();
        List<WOrderVO> list = wOrderService.selectWOrderList(wOrder);
        return getDataTable(list);
    }

    /**
     * 导出物料订单列表
     */
//    @PreAuthorize("@ss.hasPermi('system:order:export')")
//    @Log(title = "物料订单", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(WOrder wOrder)
//    {
//        List<WOrder> list = wOrderService.selectWOrderList(wOrder);
//        ExcelUtil<WOrder> util = new ExcelUtil<WOrder>(WOrder.class);
//        return util.exportExcel(list, "物料订单数据");
//    }

    /**
     * 获取物料订单详细信息
     */
    @ApiOperation(" 获取物料订单详细信息")
    @GetMapping(value = "getInfo/{id}")
    public R<WOrderVO> getInfo(@PathVariable("id") Long id)
    {
        WOrderVO selectWOrderById = wOrderService.selectWOrderById(id);
		return R.success(selectWOrderById);
    }


 
    /**
     * 删除物料订单
     */
    @ApiOperation("  删除物料订单   ")
    @GetMapping("deleteWOrderById/{id}")
    public R remove(@PathVariable Long id)
    {
        return wOrderService.deleteWOrderById(id);
    }
    
    @ApiOperation("取消物料订单")
    @GetMapping("cancelWOrderById/{id}")
    public R cancelWOrderById(@PathVariable Long id)
    {
        return wOrderService.cancelWOrderById(id);
    }
}
